// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
// const {myFun, substr} = require("demo-utils")
const { giveMsg, giveCode, useResult, result } = require("demo-utils");

module.exports = {
	_before: function () { // 通用预处理器
		this.startTime = Date.now()
		let body = this.getHttpInfo().body
		if (!body) {
			throw new useResult(400, "required")
		}
		this.params = JSON.parse(this.getHttpInfo().body)
	},
	async get() {
		let {num, page} = this.params
		if (!num || !page) {
			throw new useResult(400, "required")
		}
		let res = await db.collection("demoArt").limit(num).get()
		// let resutl = {
		// 	errCode: giveCode(0),
		// 	errMsg: giveMsg("success"),
		// 	data: res.data
		// }
		// return resutl
		return new useResult(0, "success", res.data, 0)
	},
	
	async add() {
		let {title, author} = this.params
		let res = await db.collection("demoArt").add({
			title,
			author,
			hits: Math.floor(Math.random()*100)
		})
		let resutl = {
			errCode: 0,
			errMsg: "查询成功",
			data: res
		}
		return resutl
	},
	_after: function(error, result) {
		if (error) {
			throw error
		}
		let timeCoast = Date.now() - this.startTime
		result.timeCoast = timeCoast
		return result
	}
}
